{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b9fb4da3-d18a-49d5-a572-fa71a04088a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d91b86b6-81e7-462e-96c2-02a2472405cb",
   "metadata": {},
   "source": [
    "## 一个最简单的 Series\n",
    "### 可以看到，它和list很像，不传 index参数的话，会有一个默认的索引\n",
    "### 左边是索引，右边是值。 dtype:int64 表示数据类型是整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6d056411-8f41-465b-814c-809d6e3b4a5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    10\n",
       "1    20\n",
       "2    30\n",
       "3    40\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=pd.Series([10,20,30,40])\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55095061-be92-4cf4-bf46-4b9172e60835",
   "metadata": {},
   "source": [
    "## 指定索引创建一个 Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5a4a2391-3565-4caa-84ca-98492a1aa4a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    10\n",
       "b    20\n",
       "c    30\n",
       "d    40\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=pd.Series([10,20,30,40],index=[\"a\",\"b\",\"c\",\"d\"])\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f777ed3-f0d6-4a12-8d32-9151898bbcab",
   "metadata": {},
   "source": [
    "## 取值\n",
    "### s[\"b\"]  它返回的是一个NumPy 标量对象，因为 Pandas底层会依赖Numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "feedba3b-6b1f-45a4-9060-872bd3c6263a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(20)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[\"b\"] "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5059217-af16-4cb0-8a72-dd19a856a5be",
   "metadata": {},
   "source": [
    "### print打印时，会把它转成字符串输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "77f8e007-b9d3-4c2c-a9a8-dfb315071bc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n"
     ]
    }
   ],
   "source": [
    "print(s[\"b\"]) # "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a31e01c8-0f67-480f-bb6e-02f7f5ea8ce3",
   "metadata": {},
   "source": [
    "## 向量\n",
    "### 物理中的向量指即有大小，又有方向的量。\n",
    "### 计算机、线性代数中的向量更广义，通常表示一组有序的数\n",
    "### 数学中 (1,2,3) + (4,5,6) =(5,7,9)，这就是向量运算\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2da5aabd-b9f1-4b17-84f4-4c8015a3ad24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n"
     ]
    }
   ],
   "source": [
    "a=np.array([1,2,3])\n",
    "b=np.array([4,5,6])\n",
    "print(a+b)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5242ad90-d33b-4ea2-83f9-7908fcfada8f",
   "metadata": {},
   "source": [
    "## Series中的向量化操作\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f6022928-8fa8-4e2e-979f-318e65284d52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    15\n",
      "b    25\n",
      "c    35\n",
      "d    45\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(s+5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d3e7c410-c1e9-4576-9c49-0ae9bb56429c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "my_idx\n",
       "id1              100\n",
       "20                 a\n",
       "third    {'dic1': 5}\n",
       "Name: my_name, dtype: object"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2=pd.Series(\n",
    "    data=[100,'a',{'dic1':5}],# 数据部份，这里给了不同类型的三个数\n",
    "    index=pd.Index( ['id1',20,'third'],name='my_idx' ), # 通过 pd.Index方法，不仅给了索引的值，还给它了一个名字\n",
    "    dtype='object', #代表混合类型，这里不写也可以，Pandas会自动推断为 object类型\n",
    "    name = 'my_name' # 给整个 Series 起了个名字 my_name\n",
    ")\n",
    "s2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7060dc9-e1ab-40a5-a0a8-9befaa037d13",
   "metadata": {},
   "source": [
    "## 通过.的方式 来获取属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d821082f-0dc4-43e4-9c3a-5d48d0b16356",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([100, 'a', {'dic1': 5}], dtype=object)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "34af3a8f-0098-4b8d-95d3-7b7855766db8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['id1', 20, 'third'], dtype='object', name='my_idx')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a920152c-e7cb-4bd0-9d25-121f0057b02d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('O')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "54d5b18c-3dc9-4265-a588-050b67ac9ec9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'my_name'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "bd3d69b9-dc3a-4a9b-a30f-8fb8f6151c1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.shape #获取序列长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "969e9d78-6bfc-4543-bd34-a2c4500300eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2['id1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ae184d7-ce57-418e-b49a-192957241b74",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
